package m.jclecai.com.activity;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.bumptech.glide.Glide;
import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import m.jclecai.com.R;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.base.recyclerview.BaseRecyclerViewAdapter;
import m.jclecai.com.base.recyclerview.BaseViewHolder;
import m.jclecai.com.bean.AdvertBean;
import m.jclecai.com.bean.RecordsBean;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.utils.MyLog;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;
import m.jclecai.com.view.AutoSwipeRefreshLayout;

/**
 * home页面的这里是活动的Acitivty
 */
public class EventActivity extends BaseActivity implements  SwipeRefreshLayout.OnRefreshListener{


    @BindView(R.id.rv_event)
    RecyclerView mRvEvent;


    public List<AdvertBean> mEventDatas;
    @BindView(R.id.rl_refresh_layout)
    AutoSwipeRefreshLayout mRlRefreshLayout;
    private MyAdapter mMyAdapter;

    @Override
    protected int getContentView() {
        return R.layout.activity_event;
    }


    @Override
    protected void initView() {
        setTitle("活动");
        leftOptionText("");
    }

    @Override
    protected void initData() {
        mEventDatas = new ArrayList<>();
        mMyAdapter = new MyAdapter(null);

        mRlRefreshLayout.setOnRefreshListener(this);
        mRlRefreshLayout.setRefreshing(true);

        mRvEvent.setLayoutManager(new LinearLayoutManager(this));
        mRvEvent.setAdapter(mMyAdapter);

        requestBannerData();
    }

    @Override
    protected void initListener() {

    }



    @Override
    public void onRefresh() {
        requestBannerData();
    }

    public class MyAdapter extends BaseRecyclerViewAdapter<AdvertBean> {

        public MyAdapter(List<AdvertBean> DList) {
            super(DList);
        }

        @Override
        public int getItemView() {
            return R.layout.item_event;
        }

        @Override
        protected void bindViewHolder(BaseViewHolder holder, final AdvertBean bean, int position) {

            Glide.with(EventActivity.this)
                    .load(bean.getImage())
                    .asBitmap()
                    .placeholder(R.drawable.my_head_de)
                    .error(R.drawable.my_head_de)
                    .into(holder.getImageView(R.id.iv_event));

            holder.getImageView(R.id.iv_event).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    EventActivity.this.startActivity(WebHelperActivity.getInstance(EventActivity.this,bean.getUrl(),false,bean.getName()));
                }
            });
        }
    }


    void requestBannerData() {
        HttpManager.getAsyn(Constant_Url.ADVERT_URL, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                MyLog.i(request.toString());
                if (mRlRefreshLayout != null)
                    mRlRefreshLayout.setRefreshing(false);
            }

            @Override
            public void onResponse(String response) {
                if (mRlRefreshLayout != null)
                    mRlRefreshLayout.setRefreshing(false);
                RecordsBean recordsBean = FastOk.get(response, RecordsBean.class);
                List<AdvertBean> records = recordsBean.getRecords();
                if(null!=records&&records.size()>0&&!records.equals("[]")){
                    mMyAdapter.refresh(recordsBean.getRecords());
                }
            }
        });
    }
}
